package kau.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kau.dao.AttendInfoDAOImpl;
import kau.dao.CourseDAOImpl;
import kau.dao.CustomerDAOImpl;
import kau.dao.RegisterationDAOImpl;
import kau.vo.AttendInfo;
import kau.vo.Course;
import kau.vo.Customer;
import kau.vo.Registeration;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import common.DateUtil;

public class RegisterationAction extends MultiActionController {

	public ModelAndView insert(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();

		int courseId = Integer.parseInt(request.getParameter("courseId"));

		String custId = request.getParameter("custId");
		int isConfirm = Integer.parseInt(request.getParameter("isConfirm"));
		System.out.println("courseId : " + courseId + "custId : " + custId
				+ "isConfirm : " + isConfirm);

		Registeration reg = new Registeration();
		reg.setCustId(custId);
		reg.setCourseId(courseId);
		reg.setIsConfirm(isConfirm);
		System.out.println("수강신청등록 접근");
		if (!RegisterationDAOImpl.getInstance().isExist(reg)) {
			System.out.println("수강 신청");
			RegisterationDAOImpl.getInstance().insert(reg);
		}

		nextPage.setViewName("enroll.registeration");

		return nextPage;

	}

	//
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView nextPage = new ModelAndView();

		System.out.println("rg update");

		// update
		int courseId = Integer.parseInt(request.getParameter("courseId"));
		String custId = request.getParameter("custId");
		int isConfirm = 1;

		Registeration regist = RegisterationDAOImpl.getInstance()
				.getRegisteration(custId, courseId);

		regist.setCustId(custId);
		regist.setCourseId(courseId);
		regist.setIsConfirm(isConfirm);

		String startDate = CourseDAOImpl.getInstance().getCourse(courseId)
				.getStartDate();
		String endDate = CourseDAOImpl.getInstance().getCourse(courseId)
				.getEndDate();

		int dayCount = DateUtil.getDayCount(startDate, endDate);
		System.out.println("rg update dayCount:" + dayCount);
		String nextDate = startDate;

		int custLevel = CustomerDAOImpl.getInstance().getCustomer(custId)
				.getCustLevel();

		if (custLevel == 3) {
			for (int i = 0; i < dayCount; i++) {

				if (!DateUtil.returnBoolean(nextDate)) {
					nextDate = DateUtil.getNextDay(nextDate);
					continue;
				}

				AttendInfo ai = new AttendInfo();
				ai.setCustId(custId);
				ai.setCourseId(courseId);
				ai.setDay(nextDate);
				ai.setCheckInState(1);
				ai.setCheckOutState(1);
				ai.setCheckInTime("00:00");
				ai.setCheckOutTime("00:00");
				System.out.println(ai);

				AttendInfoDAOImpl.getInstance().insert(ai);

				nextDate = DateUtil.getNextDay(nextDate);
			}
		}
		RegisterationDAOImpl.getInstance().update(regist);
		nextPage.setViewName("confirm.registeration");
		return nextPage;

	}

	//

	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView nextPage = new ModelAndView();
		int courseId = Integer.parseInt(request.getParameter("courseId"));
		String custId = request.getParameter("custId");
		Registeration reg = new Registeration();
		reg.setCustId(custId);
		reg.setCourseId(courseId);

		System.out.println("수강등록 취소 접근:" + reg);

		RegisterationDAOImpl.getInstance().delete(reg);

		nextPage.setViewName("getAllCourseId.registeration");

		return nextPage;

	}

	//
	public ModelAndView getAllRegistrations(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView nextPage = new ModelAndView();

		return nextPage;

	}

	//
	public ModelAndView getAllCourseId(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		System.out.println("getAllCourseId");

		ModelAndView nextPage = new ModelAndView();
		String custId = request.getParameter("custId");

		ArrayList<Registeration> registerList = null;

		registerList = RegisterationDAOImpl.getInstance()
				.getAllCourseId(custId);

		ArrayList<Course> courseList = new ArrayList<Course>();

		for (int i = 0; i < registerList.size(); i++) {
			Course course = CourseDAOImpl.getInstance().getCourse(
					registerList.get(i).getCourseId());
			courseList.add(course);
			System.out.println(course.getCourseName());
		}

		request.setAttribute("registerList", registerList);
		request.setAttribute("courseList", courseList);
		request.setAttribute("custId", custId);

		nextPage.setViewName("index.jsp?contentMenu=/cust/myPage.jsp&content=/cust/enrollSearch.jsp");

		return nextPage;
	}

	// 수강신청 목록 보여주는거
	public ModelAndView enroll(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		System.out.println("enroll");
		ModelAndView nextPage = new ModelAndView();

		String custId = request.getParameter("custId");

		ArrayList<Registeration> registerList = null;
		ArrayList<Integer> existList = new ArrayList<Integer>();
		ArrayList<Course> courseList = new ArrayList<Course>();

		registerList = RegisterationDAOImpl.getInstance()
				.getAllCourseId(custId);
		courseList = CourseDAOImpl.getInstance().getAllCourses();

		for (int i = 0; i < courseList.size(); i++) { // existList 를 courseList
														// 와 사이즈 같게 0으로 세팅
			existList.add(0);
		}

		for (int i = 0; i < courseList.size(); i++) {
			Registeration register = new Registeration();
			register.setCustId(custId);
			register.setCourseId(courseList.get(i).getCourseId());

			boolean result = RegisterationDAOImpl.getInstance().isExist(
					register);

			if (result) {
				existList.set(i, 1);
			}

		}

		for (int i = 0; i < existList.size(); i++) {
			System.out.println("one index " + i + " : value "
					+ existList.get(i));
		}

		request.setAttribute("registerList", registerList);
		request.setAttribute("courseList", courseList);
		request.setAttribute("custId", custId);
		request.setAttribute("existList", existList);

		nextPage.setViewName("index.jsp?contentMenu=/cust/myPage.jsp&content=/cust/enroll.jsp");

		return nextPage;

	}

	public ModelAndView getAllCustId(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView nextPage = new ModelAndView();
		System.out.println("getAllCustId");

		int courseId = Integer.parseInt(request.getParameter("courseId"));

		System.out.println(courseId);
		ArrayList<Registeration> registerList = null;

		registerList = RegisterationDAOImpl.getInstance()
				.getAllCustId(courseId);

		ArrayList<Course> courseList = new ArrayList<Course>();
		ArrayList<Customer> customerList = new ArrayList<Customer>();

		for (int i = 0; i < registerList.size(); i++) {
			Customer customer = CustomerDAOImpl.getInstance().getCustomer(
					registerList.get(i).getCustId());
			customerList.add(customer);
			Course course = CourseDAOImpl.getInstance().getCourse(
					registerList.get(i).getCourseId());
			courseList.add(course);

		}

		ArrayList<Course> allCourseList = new ArrayList<Course>();

		allCourseList = CourseDAOImpl.getInstance().getAllCourses();

		request.setAttribute("allCourseList", allCourseList);
		request.setAttribute("registerList", registerList);
		request.setAttribute("courseList", courseList);
		request.setAttribute("customerList", customerList);

		nextPage.setViewName("index.jsp?contentMenu=/admin/menuPage.jsp&content=/admin/registSearch.jsp");

		return nextPage;

	}

	// ����- ������û ����
	public ModelAndView confirm(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView nextPage = new ModelAndView();
		System.out.println("confirm");

		int courseId = Integer.parseInt(request.getParameter("courseId"));

		System.out.println(courseId);
		ArrayList<Registeration> registerList = null;

		registerList = RegisterationDAOImpl.getInstance()
				.getAllCustId(courseId);

		ArrayList<Course> courseList = new ArrayList<Course>();
		ArrayList<Customer> customerList = new ArrayList<Customer>();

		for (int i = 0; i < registerList.size(); i++) {
			Customer customer = CustomerDAOImpl.getInstance().getCustomer(
					registerList.get(i).getCustId());
			customerList.add(customer);
			Course course = CourseDAOImpl.getInstance().getCourse(
					registerList.get(i).getCourseId());
			courseList.add(course);

		}

		ArrayList<Course> allCourseList = new ArrayList<Course>();

		allCourseList = CourseDAOImpl.getInstance().getAllCourses();

		request.setAttribute("allCourseList", allCourseList);
		request.setAttribute("registerList", registerList);
		request.setAttribute("courseList", courseList);
		request.setAttribute("customerList", customerList);

		nextPage.setViewName("index.jsp?contentMenu=/admin/menuPage.jsp&content=/admin/registConfirm.jsp");

		return nextPage;

	}
}
